package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.KSQLJoinWindow;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    //phone: "18611622426"
    //发送短信验证码
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String, String> map) {

        //1. 接收参数
        String phone = map.get("phone");

        //2. 生成一个验证码 todo 临时写死验证,等到上线再放开
       //String code = RandomUtil.randomNumbers(6);
        String code = "123";
        log.info("生成的验证码是:{}", code);

        //3. 发送验证码 todo 等到生成环境再放开  为了省点钱
        //smsTemplate.sendSms(phone, code);

        //4. 记录下发送的验证码,方便后期对比，现在我们用Redis缓存优化
        session.setAttribute("USER_PHONE_CODE" + phone, code);// phone:15612345678 ,123
        redisTemplate.opsForValue().set("USER_PHONE_CODE:" + phone, code,5, TimeUnit.MINUTES);

        //5, 返回信息
        return ResultInfo.success(null);
    }
    // 用户登录注册
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");
        String codeFromFront = map.get("code");

        //2. 比对验证码,失败返回错误消息
        String codeFromRedis = (String) redisTemplate.opsForValue().get("USER_PHONE_CODE:" + phone);
        if (StringUtils.isEmpty(codeFromRedis) || !StringUtils.equals(codeFromFront, codeFromRedis)) {
            return ResultInfo.error("验证码错误");
        }

        //3. 调用service查询当前手机号在库中是否存在
        User user = userService.findByPhone(phone);
        if (user != null) {
            //3-1 查询到了,代表是一个老用户登录成功了

            //3-2 查看它的状态是否处于禁用状态,如果是,直接返回错误信息
            if (user.getStatus() == 0) {
                return ResultInfo.error("用户被禁用,请联系管理员");
            }

        } else {
            //3-2 没查询到, 代表一个新的用户在进行注册操作
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);//保存用户
        }

        //4. 用户信息放入session
        session.setAttribute(Constant.SESSION_USER, user);

        //5. 返回登录成功结果
        return ResultInfo.success(null);
    }
}